import { createRouter, createWebHistory } from 'vue-router';

import EventBus from '@/utils/event-bus.js';
import DialogHandle from '@/views/components/dialog.js';
import index from '../views/index.vue';
import av from '../views/audioandvideo.vue';
import img from '../views/image.vue';
import url from '../views/url.vue';
import card from '../views/card.vue';
import file from '../views/file.vue';
import fmjg from '../views/fmjg.vue';
import form from '../views/form.vue';
import mb from '../views/mb.vue';
import editor from '../views/editor.vue';
import v3 from '../views/editor/index.vue';
import admin from '../views/admin.vue';
import scdm from '../views/admin/scdm.vue';
import sclist from '../views/admin/sclist.vue';
import sc from '../views/admin/sc.vue';
import d1 from '../views/admin/data1.vue';
import d2 from '../views/admin/data1.vue';
import ang from '../views/admin/anget.vue';
import { showToast } from '@/utils/message';
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: index,
    },
    {
      path: '/av',
      name: 'av',
      component: av,
    },
    {
      path: '/img',
      name: 'img',
      component: img,
    },
    {
      path: '/url',
      name: 'url',
      component: url,
    },
    {
      path: '/card',
      name: 'card',
      component: card,
    },
    {
      path: '/file',
      name: 'file',
      component: file,
    },
    {
      path: '/fmjg',
      name: 'fmjg',
      component: fmjg,
    },
    {
      path: '/form',
      name: 'form',
      component: form,
    },
    {
      path: '/mb',
      name: 'mb',
      component: mb,
    },
    {
      path: '/editor',
      name: 'editor',
      component: editor,
    },
    {
      path: '/admin',
      name: 'admin',
      component: admin,
    },
    {
      path: '/scdm',
      name: 'scdm',
      component: scdm,
    },
    {
      path: '/sclist',
      name: 'sclist',
      component: sclist,
    },
    {
      path: '/sc',
      name: 'sc',
      component: sc,
    },
    {
      path: '/d1',
      name: 'd1',
      component: d1,
    },
    {
      path: '/d2',
      name: 'd2',
      component: d2,
    },
    {
      path: '/ang',
      name: 'ang',
      component: ang,
    },
    {
      path: '/v3',
      name: 'v3',
      component: v3,
    },
  ],
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 假设localStorage中的登录标识是'userToken'
  console.log('from', from);
  console.log('to', to);
  const isLoggedIn = localStorage.getItem('userinfo');

  if (!isLoggedIn) {
    // 如果用户尝试访问非登录页面且未登录，则重定向到登录页面
    // next('/login');
    if (
      to.path != '/' &&
      to.path != '/pv' &&
      to.fullPath != '/editor?t=m' &&
      to.fullPath != '/av' &&
      to.fullPath != '/img' &&
      to.fullPath != '/url' &&
      to.fullPath != '/file' &&
      to.fullPath != '/card' &&
      to.fullPath != '/fmjg' &&
      to.fullPath != '/form'
    ) {
      EventBus.emit('login', '123');
      const dialog = DialogHandle({
        title: '请登录',
        content: '<div class="text-center">打开微信扫一扫登录系统</div>',
        onConfirm: () => {
          return new Promise(async (resolve) => {
            dialog.showLoading();
            setTimeout(() => {
              dialog.hideLoading();
              resolve();
            }, 1500);
          });
        },
      });
    } else {
      next();
    }
  } else {
    // 如果用户已登录或尝试访问登录页面，则继续跳转
    next();
  }
});

export default router;

